<template>
  <div class="mod-config">
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;"
      height="75vh"
    >
      <el-table-column
        prop="time"
        header-align="center"
        align="center"
        label="时间"
      />
      <el-table-column
        prop="value"
        header-align="center"
        align="center"
        label="流量值"
      />
      <el-table-column
        prop="state"
        header-align="center"
        align="center"
        label="状态"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.state === '正常数据'">{{
            scope.row.state
          }}</span>
          <span
            style="color: #f00;font-weight: 600;"
            v-else-if="scope.row.state === '突变修正'"
            >{{ scope.row.state }}</span
          >
          <span
            style="color: #00b7ee;font-weight: 600;"
            v-else-if="scope.row.state === '负数修正'"
            >{{ scope.row.state }}</span
          >
          <span
            style="color: #5daf34;font-weight: 600;"
            v-else-if="scope.row.state === '空数修正'"
            >{{ scope.row.state }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        width="150"
        v-if="isAuth('cms:tbFlowState:update')"
        header-align="center"
        align="center"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            @click="addOrUpdateHandle(scope.row.id)"
            type="text"
            v-if="isAuth('cms:tbFlowState:update')"
            >修改数据</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      style="margin: 15px 0;"
      layout="total, sizes, prev, pager, next, jumper"
    />
    <!-- 新增、修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="reFreshDatalist"
    />
  </div>
</template>

<script>
import moment from "moment";
import AddOrUpdate from "./state-flow-add-or-update";
export default {
  data() {
    return {
      dataForm: {
        equipmentId: null,
        time: [
          moment(new Date()).format("YYYY-MM-DD"),
          moment(new Date()).format("YYYY-MM-DD")
        ]
        // name: ""
      },
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataList: [], // 日常抄表爬取数据列表
      warningDataList: [], // 日常抄表爬取数据列表中存在未赋值的列表
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      dailyVisible: false,
      uploadVisible: false,
      stationOptions: []
    };
  },
  components: {
    AddOrUpdate
  },
  methods: {
    // 获取数据列表
    init(timeGroup, equirementId, type) {
      this.dataListLoading = true;
      this.dataForm.equipmentId = equirementId;
      this.dataForm.time = timeGroup;
      if (type === 1) {
        this.pageIndex = 1;
      }
      if (
        this.dataForm.equipmentId === null ||
        this.dataForm.equipmentId === ""
      ) {
        this.$message.error("请选择流量计");
        this.dataListLoading = false;
        return;
      } else if (this.dataForm.time.length < 2) {
        this.$message.error("请选择查询的时间段");
        this.dataListLoading = false;
        return;
      } else {
        this.$http({
          url: this.$http.adornUrl("/cms/tbFlowState/list"),
          method: "get",
          params: this.$http.adornParams({
            page: this.pageIndex,
            limit: this.pageSize,
            equipmentId: this.dataForm.equipmentId,
            startDateTime: this.dataForm.time[0] + " 00:00:00",
            endDateTime: this.dataForm.time[1] + " 23:59:59"
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list; // 获取日常抄表的数据进行赋值
            this.totalPage = data.page.totalCount;
          } else {
            this.dataList = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
      }
    },
    // 刷新页面数据
    reFreshDatalist() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/cms/tbFlowState/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          equipmentId: this.dataForm.equipmentId,
          startDateTime: this.dataForm.time[0] + " 00:00:00",
          endDateTime: this.dataForm.time[1] + " 23:59:59"
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list; // 获取日常抄表的数据进行赋值
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.reFreshDatalist();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.reFreshDatalist();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.nameTil {
  padding: 0;
  margin: 0 0 10px;
  font-size: 16px;
  font-weight: 600;
}
</style>
